<template>
   <div class="exchange-page">
     <assets-head :title="$t('闪兑')" >
        <img src="../../assets/image/assets-center/exchange.png" alt="exchange-img" class="w-44 h-38" @click="$router.push('/exchange/exchangeHistory')"/>
     </assets-head>
     <list-account show class="mt-94" type="exchange" @exchange="onPopup" ref="exchange"/>
      <van-popup v-model="show" position="bottom" :close-on-click-overlay="false" @close="onClose">
            <div class="pop-box relative mainBackground">
                <img src="@/assets/image/icon-close.png" class="w-38 h-38 absolute right-20 top-20" @click="show=false"/>
                <p class="font-38 text-center pt-60 textColor">{{ $t('确认报价') }}</p>
                <p class="font-28 mt-49 text-grey text-center">{{ $t('扣除手续费后,您将获得') }}</p>
                <p class="font-50 mt-23 text-center textColor">{{detail.get_volume }}  <span class="font-35"> {{detail.symbol_to && detail.symbol_to.toUpperCase()}}</span></p>
                <ul class="mx-32 px-30 pt-20 pb-34  tab_background mt-53 rounded">
                    <li class="flex justify-between font-32 mt-25">
                        <span class="text-grey">{{ $t('闪兑自') }}</span>
                        <span class="textColor">{{detail.volume}} {{detail.symbol && detail.symbol.toUpperCase()}}</span>
                    </li>
                    <li class="flex justify-between font-32 mt-25">
                        <span class="text-grey">{{ $t('汇率') }}</span>
                        <span class="textColor">1 {{detail.symbol && detail.symbol.toUpperCase()}}≈{{(detail.rate*1).toFixed(5)}} {{detail.symbol_to && detail.symbol_to.toUpperCase()}}</span>
                    </li>
                </ul>
                <div class="h-100 rounded flex justify-between px-32 box-border items-center mt-56 mb-48 text-white w-full">
                    <p class="w-360 h-80 rounded flex justify-center items-center bgDark" @click="show = false">
                      {{ $t('返回') }}</p>
                    <p class="w-360 h-80 rounded btnMain flex justify-center items-center text-black" @click="onConfirm">
                      {{ $t('闪兑') }}（{{count}}{{ $t('秒') }}）</p>
                </div>
            </div>
        </van-popup>
   </div>
</template>

<script>
import assetsHead from "@/components/assets-head";
import ListAccount from '@/components/mining-account'
import { _initExchange, _exchange } from "@/API/fund.api";
import { Popup } from "vant"
export default {
    components: {
        assetsHead,
        ListAccount,
        [Popup.name]: Popup
    },
    data() {
        return {
            show: false,
            session_token: '',
            detail: {},
            interval: null,
            count: 5
        }
    },
    created() {
       this.getSessionToken()
    },
    methods:{
        getSessionToken() { // 获取凭证
            _initExchange().then(data => {
                const { session_token } = data
                console.log(data)
                this.session_token = session_token
            })
        },
        onPopup(evt) {
            this.clearInterval()
            this.detail = evt
            this.show = true
            this.count = 5
            this.interval = setInterval(() => {
                this.count--
                if (this.count <= 0) {
                    this.clearInterval()
                    this.$toast(this.$t('汇率已变化,请重新确认'))
                    this.show = false
                }
            }, 1000)
        },
        onClose() {
            this.$refs.exchange.fetchRate(true)
            this.clearInterval()
        },
        onClickLeft() {
            this.$router.go(-1)
        },
        onConfirm() { // 闪兑
            this.clearInterval()
           _exchange({...this.detail, session_token: this.session_token}).then(() => {
                this.$router.push('/exchange/exchangeSubmit?data=' + JSON.stringify(this.detail))
                this.getSessionToken()
                this.show = false
           }).catch(() => {
                this.getSessionToken()
                this.show = false
           })


        },
        clearInterval() {
            if (this.interval) {
                clearInterval(this.interval)
                this.interval = null
            }
        }
    },
    beforeRouteLeave(to, from, next) {
        this.clearInterval()
        next()
    }
}
</script>
<style>
.van-popup.van-popup--bottom {
    border-top-left-radius: 30px;
    border-top-right-radius: 30px;
}
</style>
<style lang="scss" scoped>
.bg-grey-light {
    background: #F5F5F5;
}
.bg-grey-dark {
    background: #C8CAD2;
}
</style>
